/**
 * Created by huangsanm on 2017/6/28.
 */
var takeout = function () {

    var isCheckAll = false

    var openMenu = function (url, data) {
        //alert(url);
        $.ajax({
            url: url,
            type: 'GET',
            data: data,
            success: function (res) {
                $('#manage_content').html(res);
            },
            error: function (res) {
                $('#manage_content').html(res);
            },
        })
    };
    // 全选或者不全选
    var selectAll = function () {
        if (isCheckAll) {
            $("input[type='checkbox']").each(function (e) {
                console.log(e)
                this.checked = false;
            });
            isCheckAll = false;
        } else {
            $("input[type='checkbox']").each(function (e) {
                this.checked = true;
            });
            isCheckAll = true;
        }
    };
    // 全选或者不全选
    var selectIframeAll = function () {
        if (isCheckAll){
            $("#ifid1").contents().find(":checkbox").each(function (e) {
                console.log(e)
                this.checked = false;
            });
            isCheckAll = false;
        } else {
            $("#ifid1").contents().find(":checkbox").each(function (e) {
                this.checked = true;
            });
            isCheckAll = true;
        }
    };

    var openEditHtml = function (url, data) {
        $.ajax({
            url: url,
            type: 'GET',
            data: data,
            success: function (res) {
                $('#manage_content').html(res);
            },
            error: function (res) {
                $('#manage_content').html(res);
            },
        })
    };

    var ulLiA = function (e) {
        var ulLiA = e.id
        $('#navClassify>li>a').removeClass("backg");
        $('#' + ulLiA).addClass("backg");
    }
    var open = function (e) {
        var aa = e.id
        console.log(aa)
        $('#side-menu>li').removeClass("action");
        $('.greensClassify').removeClass('action');
        $('#' + aa).addClass("action");
    }

    /**
     * ajax提交表单
     * @param formId
     * @param dataType
     * @param callback
     * @returns {string}
     */
    var ajaxForm = function (formId, dataType, callback) {
        if(dataType == undefined || dataType == null){
            dataType = "html";
        }

        var result = "";
        $("#" + formId).ajaxSubmit({
            dataType : dataType,
            async: false,
            cache: false,
            success:  function (data) {
                /*if(data.indexOf("loginForm") != -1){ getOpenUserId
                    window.location.href = cxt + "/platform/login";
                    return;
                }*/
                result = data;

                //扩展回调函数
                if( callback != null ){
                    callback();
                }
            }
        });
        return result;
    }

    var ajaxFormMainPanel = function(formId, callback){
        var result = ajaxForm(formId, callback);
        $("#manage_content").html(result);
    };


    var ajaxFormPopupBox = function(formId, callback){
        var result = ajaxForm(formId, callback);
        $("#ifid1").html(result);
    };

    var sendMessage = function(formId, callback){
        PostbirdAlertBox.confirm({
            'title': '发送消息模版 ',
            'content': '是否确认发送',
            'okBtn': '确认',
            'contentColor': 'red',
            'onConfirm': function () {
                var result = ajaxForm(formId, callback);
                $("#").html(result);
                // alert("Cancel-回调触发后隐藏提示框");
            },
            'onCancel': function () {
                console.log("Cancel-回调触发后隐藏提示框");
                //alert("Cancel-回调触发后隐藏提示框");
            }
        });
    }


    //删除
    var del = function (url, target, args) {
        data = args;
        if(target != 0){
            $("input[type='checkbox']").each(function (e) {
                if(this.checked){
                    data += this.value + ",";
                }
            });
            data = data.substring(0, data.length - 1);
        }
        if(data == '' || data == null || data == undefined){
            //alert(data);
            PostbirdAlertBox.confirm({
                'title': ' ',
                'content': '请选择需要删除的内容',
                'okBtn': '确认',
                'contentColor': 'red',
                'onConfirm': function () {

                },
                'onCancel': function () {
                    console.log("Cancel-回调触发后隐藏提示框");
                    //alert("Cancel-回调触发后隐藏提示框");
                }
            });
        }else{
            //alert(data);
            PostbirdAlertBox.confirm({
                'title': ' ',
                'content': '是否确认删除',
                'okBtn': '确认',
                'contentColor': 'red',
                'onConfirm': function () {
                    $.ajax({
                        url: url,
                        type: 'GET',
                        data: {
                            ids:data
                        },
                        success: function (res) {
                            $('#manage_content').html(res);

                        },
                        error: function (res) {
                            $('#manage_content').html(res);

                        }
                    })
                },
                'onCancel': function () {
                    console.log("Cancel-回调触发后隐藏提示框");
                    //alert("Cancel-回调触发后隐藏提示框");
                }
            });
        }

    }
    //同步
    var copy = function (url, target, args) {
        data = "";
        tablename = args;
        if(target != 0){
            $("input[type='checkbox']").each(function (e) {
                if(this.checked){
                    data += this.value + ",";
                }
            });
            data = data.substring(0, data.length - 1);
        }
        if((data == '' || data == null || data == undefined)&&(tablename!="sychronize_all")){
            //alert(data);
            PostbirdAlertBox.confirm({
                'title': ' ',
                'content': '请选择需要同步的内容',
                'okBtn': '确认',
                'contentColor': 'red',
                'onConfirm': function () {

                },
                'onCancel': function () {
                    console.log("Cancel-回调触发后隐藏提示框");
                    //alert("Cancel-回调触发后隐藏提示框");
                }
            });
        }else{
            $.ajax({
                url: url,
                type: 'GET',
                data: {
                    content_ids:data,
                    table:tablename,
                },
                success: function (res) {
                    //alert(data);
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        shadeClose: true,
                        skin: 'layui-layer-rim',
                        area : ['1080px' , '760px'],
                        id: 'ifid1',
                        content: res
                    });
                },
                error: function (res) {

                }
            });
        }

    }


    //同步
    var copyByGender = function (url, target, args) {
        data = "";
        tablename = args;
        if(target != 0){
            $("input[type='checkbox']").each(function (e) {
                if(this.checked){
                    data += this.value + ",";
                }
            });
            data = data.substring(0, data.length - 1);
        }
        if((data == '' || data == null || data == undefined)){
            //alert(data);
            PostbirdAlertBox.confirm({
                'title': ' ',
                'content': '请选择需要同步的内容',
                'okBtn': '确认',
                'contentColor': 'red',
                'onConfirm': function () {

                },
                'onCancel': function () {
                    console.log("Cancel-回调触发后隐藏提示框");
                    //alert("Cancel-回调触发后隐藏提示框");
                }
            });
        }else{
            $.ajax({
                url: url,
                type: 'GET',
                data: {
                    copyType:'copyByGender',
                    content_ids:data,
                    table:tablename,
                },
                success: function (res) {
                    //alert(data);
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        shadeClose: true,
                        skin: 'layui-layer-rim',
                        area : ['1080px' , '760px'],
                        id: 'ifid1',
                        content: res
                    });
                },
                error: function (res) {

                }
            });
        }

    }

    //同步
    var copyByGenderCategory = function (url, target, args) {
        data = "";
        tablename = args;
        if(target != 0){
            $("input[type='checkbox']").each(function (e) {
                if(this.checked){
                    data += this.value + ",";
                }
            });
            data = data.substring(0, data.length - 1);
        }
        if((data == '' || data == null || data == undefined)){
            //alert(data);
            PostbirdAlertBox.confirm({
                'title': ' ',
                'content': '请选择需要同步的内容',
                'okBtn': '确认',
                'contentColor': 'red',
                'onConfirm': function () {

                },
                'onCancel': function () {
                    console.log("Cancel-回调触发后隐藏提示框");
                    //alert("Cancel-回调触发后隐藏提示框");
                }
            });
        }else{
            $.ajax({
                url: url,
                type: 'GET',
                data: {
                    copyType:'copyByGenderCategory',
                    content_ids:data,
                    table:tablename,
                },
                success: function (res) {
                    //alert(data);
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        shadeClose: true,
                        skin: 'layui-layer-rim',
                        area : ['1080px' , '760px'],
                        id: 'ifid1',
                        content: res
                    });
                },
                error: function (res) {

                }
            });
        }

    }

//同步，按照类型类别
    var copyByGenderCategoryType = function (url, target, args) {
        data = "";
        tablename = args;
        if(target != 0){
            $("input[type='checkbox']").each(function (e) {
                if(this.checked){
                    data += this.value + ",";
                }
            });
            data = data.substring(0, data.length - 1);
        }
        if((data == '' || data == null || data == undefined)){
            //alert(data);
            PostbirdAlertBox.confirm({
                'title': ' ',
                'content': '请选择需要同步的内容',
                'okBtn': '确认',
                'contentColor': 'red',
                'onConfirm': function () {

                },
                'onCancel': function () {
                    console.log("Cancel-回调触发后隐藏提示框");
                    //alert("Cancel-回调触发后隐藏提示框");
                }
            });
        }else{
            $.ajax({
                url: url,
                type: 'GET',
                data: {
                    copyType:'copyByGenderCategoryType',
                    content_ids:data,
                    table:tablename,
                },
                success: function (res) {
                    //alert(data);
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        shadeClose: true,
                        skin: 'layui-layer-rim',
                        area : ['1080px' , '760px'],
                        id: 'ifid1',
                        content: res
                    });
                },
                error: function (res) {

                }
            });
        }

    }

    //同步
    var copyTable = function (url, target, args) {
        tablename = args;
            $.ajax({
                url: url,
                type: 'GET',
                data: {
                    copyType:tablename,
                    table:tablename,
                },
                success: function (res) {
                    //alert(data);
                    layer.open({
                        type: 1,
                        title: false,
                        closeBtn: 1,
                        shadeClose: true,
                        area : ['800px' , '520px'],
                        skin: 'yourclass',
                        id: 'ifid1',
                        content: res
                    });
                },
                error: function (res) {

                }
            });

    }
    //同步
    var updatedb = function (url, target, args) {
        ui_checkbox = "";
        data = "";
        var type=args;
        if(target != 0){
            $("input[type='checkbox']").each(function (e) {
                if(this.value=='on'||this.value=='off') {
                    ui_checkbox += this.checked ;
                }else {
                    if(this.checked){
                        data += this.value + ",";
                    }
                }
            });
            data = data.substring(0, data.length - 1);
            $(".layui-input-block.input[type='checkbox']").first(function (e) {
                    ui_checkbox += this.value ;
            });


        }
        if(data == '' || data == null || data == undefined){
            //alert(data);
            PostbirdAlertBox.confirm({
                'title': ' ',
                'content': '请选择需要同步的数据库',
                'okBtn': '确认',
                'contentColor': 'red',
                'onConfirm': function () {
                },
                'onCancel': function () {
                    console.log("Cancel-回调触发后隐藏提示框");
                    //alert("Cancel-回调触发后隐藏提示框");
                }
            });
        }else {

            //询问框
            layer.confirm('同步后会覆盖相同id数据且不可恢复，是否开始同步？', {
                btn: ['确认', '取消'] //按钮
            }, function () {
                layer.load();
                $.ajax({
                    url: url,
                    type: 'GET',
                    data: {
                        ui_checkbox:ui_checkbox,
                        ids:data,
                        type:type,
                    },
                    success: function (res) {
                        //alert(data);
                        // layer.closeAll('loading');
                        layer.closeAll();
                        if( res.indexOf('error') >= 0) {
                            layer.msg('请选择要同步的数据库', {
                                icon: 16
                                ,shade: 0.01
                            });
                        }else {
                            layer.msg('数据库同步成功', {
                                icon: 16
                                ,shade: 0.01
                            });
                        }
                    },
                    error: function (res) {
                        layer.closeAll('loading');
                        layer.msg('数据库同步失败', {
                            icon: 16
                            ,shade: 0.01
                        });
                    }
                });


            }, function () {
            });
        }
    }
    var updateStatus = function (url, id, val) {
        $.ajax({
            url: url,
            type: 'GET',
            data: {
                src_id:id,
                src_val: val
            },
            success: function (res) {
                $('#manage_content').html(res);
            },
            error: function (res) {
                $('#manage_content').html(res);
            },
        })
    }

    var addOptions = function (id) {
        var content = '<div class="articleFormLabel">\n' +
            '            <label class="mainSetConsistentWidth">选项：</label>\n' +
            '            <input type="text" name="item" class="mainSetWidth-options" placeholder="选项"/>\n' +
            '            <input type="text" name="score" class="mainSetWidth-options-score" placeholder="分数"/>\n' +
            '            <label class="mainSetConsistentWidth">' +
            '<a href="javascript:void();" onclick="takeout.delOptions(this)">删除</a></label>\n' +
            '        </div>';
        $('#' + id).append(content);
    }

    var delOptions = function (obj) {
        $(obj).parent().parent().remove();
    }

    var loadOptions = function (id, lables, score) {
        alert(lables);
    }


    return {
        openMenu: openMenu,
        selectAll: selectAll,
        openEdit: openEditHtml,
        ulLiA: ulLiA,
        open: open,
        ajaxFormMainPanel: ajaxFormMainPanel,
        del: del,
        copy: copy,
        copyByGender:copyByGender,
        copyByGenderCategory:copyByGenderCategory,
        copyByGenderCategoryType:copyByGenderCategoryType,
        updatedb: updatedb,
        updateStatus:updateStatus,
        addOptions:addOptions,
        delOptions:delOptions,
        loadOptions:loadOptions,
        sendMessage:sendMessage,
        selectIframeAll:selectIframeAll,
        copyTable:copyTable,
        ajaxFormPopupBox:ajaxFormPopupBox
    };

}()
